const path = require("path");
const fs = require("fs");
const { assert } = require("console");
const { connect } = require("tls");

const SYNC_DIR = path.join(__dirname, "now.json");
const fileTreeForNow = fs.readFileSync(SYNC_DIR, {
  encoding: "utf8",
  flag: "r",
});

const obj = JSON.parse(fileTreeForNow);
// console.log(obj.children[0].children)
const childs = obj.children;

const timeLimit = Date.now() - 8000 * 288000;
console.log(timeLimit)
// childs.forEach(element => {
//     if(element.mtime < timeLimit){

//     }
//     console.log(element);
// });

function searchFilesWithQuery(childs, query, timeLimit) {
  const results = [];
  // console.log(childs);
  for (const element of childs) {
    if (element.mtime < timeLimit) {
      // console.log('here')
      continue;
    }
    if (element.name.toLowerCase().includes(String(query).toLowerCase())) {
      if (element.type === "directory") {
        const children = getAllFiles(element.children); // 递归获取子目录内容
        results.push({
          name: element.name,
          type: element.type,
          path: element.path,
          children,
        });
      }
    } else {
      results.push({
        name: element.name,
        type: element.type,
        path: element.path,
        children: [],
      });
    }
  }
  return results;
}

function getAllFiles(childs) {
  const temp = [];
  for (const element of childs) {
    if (element.type === "directory") {
      const children = getAllFiles(element.children); // 递归获取子目录内容
      temp.push({
        name: element.name,
        type: element.type,
        path: element.path,
        children,
      });
    } else {
      temp.push({
        name: element.name,
        type: element.type,
        path: element.path,
        children: [],
      });
    }
  }
  return temp;
}
const res = searchFilesWithQuery(childs, "One", timeLimit);
console.log(res);
console.log(res[0].children);
console.log(res[0].children[0]);
